﻿using sx_windows.data.bean;
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace sx_windows.data.repository.source
{
    class LocalSource
    {
        private static string CONFIG_XML = "config.xml";
        private static string CONFIG_ROOT = "config";
        //shanxunConfig
        private static string SHANXUN_ROOT = "shanxun";
        private static string SHANXUN_USER = "sxUser";
        private static string SHANXUN_PASSWORD = "sxPassword";
        private static string SEND_HEART = "sendHeart";
        //routerConfig
        private static string ROUTER_ROOT = "router";
        private static string ROUTER_ADDRESS = "address";
        private static string ROUTER_USER = "user";
        private static string ROUTER_PASSWORD = "password";
        //wifiConfig
        private static string WIFI_ROOT = "wifi";
        private static string WIFI_NAME = "name";
        private static string WIFI_PASSWORD = "password";

        private XmlDocument xmldoc;

        public LocalSource()
        {
            if (!File.Exists(CONFIG_XML))
            {
                createXmlFile();
            }
            xmldoc = new XmlDocument();
            try
            {
                xmldoc.Load(CONFIG_XML);
            }
            catch (System.Exception)
            {
                createXmlFile();
                xmldoc.Load(CONFIG_XML);
            }

        }

        private void createXmlFile()
        {
            var d = File.Open(CONFIG_XML, FileMode.Create);
            var xDoc = new XDocument(
                new XElement(CONFIG_ROOT,
                    new XElement(WIFI_ROOT,
                        new XElement(WIFI_NAME, "www.mandroid.cn"),
                        new XElement(WIFI_PASSWORD, "12345678")
                    )
                )
               );
            xDoc.Save(d);
            d.Close();
        }

        public SxConfigBean GetSxConfig()
        {
            try
            {
                XmlElement rootElem = xmldoc.DocumentElement;
                SxConfigBean bean = new SxConfigBean();
                XmlNodeList list = rootElem.GetElementsByTagName(SHANXUN_ROOT);
                if (list.Count == 1)
                {
                    XmlElement node = (XmlElement)list[0];
                    bean.User = node.GetElementsByTagName(SHANXUN_USER)[0].InnerText;
                    bean.Password = node.GetElementsByTagName(SHANXUN_PASSWORD)[0].InnerText;
                    bean.SendHeart = Convert.ToInt16(node.GetElementsByTagName(SEND_HEART)[0].InnerText) == 1;
                }
                return bean;
            }
            catch (System.Exception)
            {

                return new SxConfigBean();
            }
            
        }

        public void SaveSxConfig(string user,string password,int needSend)
        {
            XmlElement rootElem = xmldoc.DocumentElement;
            XmlNodeList list = rootElem.GetElementsByTagName(SHANXUN_ROOT);
            if (list.Count == 1)
            {
                XmlElement node = (XmlElement)list[0];
                node.GetElementsByTagName(SHANXUN_USER)[0].InnerText=user;
                node.GetElementsByTagName(SHANXUN_PASSWORD)[0].InnerText=password;
                node.GetElementsByTagName(SEND_HEART)[0].InnerText = needSend+"";
            }
            else
            {
                XmlElement node = xmldoc.CreateElement(SHANXUN_ROOT);
                XmlElement sxUserNode = xmldoc.CreateElement(SHANXUN_USER);
                XmlElement sxPasswordNode = xmldoc.CreateElement(SHANXUN_PASSWORD);
                XmlElement sendHeartNode = xmldoc.CreateElement(SEND_HEART);
                sxUserNode.InnerText = user;
                sxPasswordNode.InnerText = password;
                sendHeartNode.InnerText = needSend+"";
                node.AppendChild(sxUserNode);
                node.AppendChild(sxPasswordNode);
                node.AppendChild(sendHeartNode);
                xmldoc.DocumentElement.AppendChild(node);
            }
            xmldoc.Save(CONFIG_XML);
        }
        public RouterConfigBean GetRouterConfig()
        {
            try
            {
                XmlElement rootElem = xmldoc.DocumentElement;
                RouterConfigBean bean = new RouterConfigBean();
                XmlNodeList list = rootElem.GetElementsByTagName(ROUTER_ROOT);
                if (list.Count == 1)
                {
                    XmlElement node = (XmlElement)list[0];
                    bean.User = node.GetElementsByTagName(ROUTER_USER)[0].InnerText;
                    bean.Password = node.GetElementsByTagName(ROUTER_PASSWORD)[0].InnerText;
                    bean.Address = node.GetElementsByTagName(ROUTER_ADDRESS)[0].InnerText;
                }
                return bean;
            }
            catch (System.Exception)
            {

                return new RouterConfigBean();
            }
        }
        public void SaveRouterConfig(string address, string user, string password)
        {
            XmlElement rootElem = xmldoc.DocumentElement;
            XmlNodeList list = rootElem.GetElementsByTagName(ROUTER_ROOT);
            if (list.Count == 1)
            {
                XmlElement node = (XmlElement)list[0];
                node.GetElementsByTagName(ROUTER_USER)[0].InnerText = user;
                node.GetElementsByTagName(ROUTER_PASSWORD)[0].InnerText = password;
                node.GetElementsByTagName(ROUTER_ADDRESS)[0].InnerText = address;
            }
            else
            {
                XmlElement node = xmldoc.CreateElement(ROUTER_ROOT);
                XmlElement routerUserNode = xmldoc.CreateElement(ROUTER_USER);
                XmlElement routerPasswordNode = xmldoc.CreateElement(ROUTER_PASSWORD);
                XmlElement routerAddressNode = xmldoc.CreateElement(ROUTER_ADDRESS);
                routerUserNode.InnerText = user;
                routerPasswordNode.InnerText = password;
                routerAddressNode.InnerText = address;
                node.AppendChild(routerAddressNode);
                node.AppendChild(routerUserNode);
                node.AppendChild(routerPasswordNode);
                xmldoc.DocumentElement.AppendChild(node);
            }
            xmldoc.Save(CONFIG_XML);
        }
        public WifiSettingBean GetWifiConfig()
        {
            try
            {
                XmlElement rootElem = xmldoc.DocumentElement;
                WifiSettingBean bean = new WifiSettingBean();
                XmlNodeList list = rootElem.GetElementsByTagName(WIFI_ROOT);
                if (list.Count == 1)
                {
                    XmlElement node = (XmlElement)list[0];
                    bean.Name = node.GetElementsByTagName(WIFI_NAME)[0].InnerText;
                    bean.Password = node.GetElementsByTagName(WIFI_PASSWORD)[0].InnerText;
                }
                return bean;
            }
            catch (System.Exception)
            {

                return new WifiSettingBean();
            }
        }
        public void SaveWifiConfig(string name, string password)
        {
            XmlElement rootElem = xmldoc.DocumentElement;
            XmlNodeList list = rootElem.GetElementsByTagName(WIFI_ROOT);
            if (list.Count == 1)
            {
                XmlElement node = (XmlElement)list[0];
                node.GetElementsByTagName(WIFI_NAME)[0].InnerText = name;
                node.GetElementsByTagName(WIFI_PASSWORD)[0].InnerText = password;
            }
            else
            {
                XmlElement node = xmldoc.CreateElement(WIFI_ROOT);
                XmlElement wifiNameNode = xmldoc.CreateElement(WIFI_NAME);
                XmlElement wifiPasswordNode = xmldoc.CreateElement(WIFI_PASSWORD);
                wifiNameNode.InnerText = name;
                wifiPasswordNode.InnerText = password;
                node.AppendChild(wifiNameNode);
                node.AppendChild(wifiPasswordNode);
                xmldoc.DocumentElement.AppendChild(node);
            }
            xmldoc.Save(CONFIG_XML);
        }
    }
}
